import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author 09lmartinelli
 */
public class MemoryGameService implements GameService {
    private final List<Game> Games = new ArrayList<>();
    private final List<Team> Teams = new ArrayList<>();
    private final List<Bet> Bets = new ArrayList<>();
    private final List<User> Users = new ArrayList<>();
    
    public MemoryGameService() {        
        User fred = new User(this, "fred_89", "Fred", "Feuerstein", "fred.feuerstein@gmail.com");
        
        this.Users.add(fred);
        
        Team fcsg =new Team("FCSG");
        Team fzh = new Team("FZH");
        Team fcb = new Team("FCB");
        Team yb = new Team("YB");
        
        this.Teams.add(fcsg);
        this.Teams.add(fzh);
        this.Teams.add(fcb);
        this.Teams.add(yb);
        
        Game game1 = new Game(fcsg, fzh, new Date());
        Game game2 = new Game(fcb, fzh, new Date());
        Game game3 = new Game(fcb, yb, new Date());
                      
        this.Games.add(game1);
        this.Games.add(game2);
        this.Games.add(game3);
              
        this.Bets.add(new Bet(fred, game1, 2, 1 ));
        this.Bets.add(new Bet(fred, game2, 3, 4 ));
                  
        game1.setResult( new GameResult(3,0));
        game2.setResult( new GameResult(3,2));        
    }
        
    @Override
    public List<Game> getGames() {
        return Games;
    }

    @Override
    public List<Team> getTeams() {
       return Teams;
    }

    @Override
    public void placeBet(Bet bet) {
        this.Bets.add(bet);
    }

    @Override
    public List<Bet> getBets(User user) {
        List<Bet> result = new ArrayList();
        for (Bet bet : this.Bets) {
            if(bet.getUser() == user) {
                result.add(bet);
            }
        }
        return result;
    }

    @Override
    public Map<String, Integer> getHighscore() {
        Map<String, Integer> result = new HashMap<>();
        for (User user : this.Users) {
            result.put(user.getUserName(), this.getPoints(user));
        }
        return result;
    }
    
    /**
     * Get the value of Points
     *
     * @return the value of Points
     */
    protected int getPoints(User user) {
        //Could be placed in stored procedure too
        List<Bet> bets = this.getBets(user);
        int pointSum = 0;
        
        for(Bet bet : bets) {
            pointSum += bet.getPoints();
        }
        
        return pointSum;
    }
    
}
